package cn.edu.cuc.yjsy.admin.dialogs;

import java.sql.SQLException;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cn.edu.cuc.yjsy.admin.connection.SourceConnection;
import cn.edu.cuc.yjsy.admin.util.MessageHelper;
import cn.edu.cuc.yjsy.admin.util.Op;

public class ImportFilePage extends WizardPage {

	private Combo tableName;
	private Text fileText;
	private String filePath;
	private boolean bOverwrite = false;

	protected ImportFilePage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
		setDescription(MessageHelper.WIZARD_IMPORT_FILEPAGE_DESCRIPTION);
	}

	public void createControl(final Composite parent) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		parent.setLayout(gridLayout);
		Label label = new Label(parent, SWT.WRAP);
		label.setText(MessageHelper.WIZARD_IMPORT_FILEPAGE_FILE);
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		data.horizontalSpan = 2;
		label.setLayoutData (data);
		fileText = new Text(parent, SWT.BORDER | SWT.READ_ONLY);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		data.widthHint = 300;
		fileText.setLayoutData(data);
		final Button browserButton = new Button(parent, SWT.PUSH);
		browserButton.setText(MessageHelper.IMPORT_BROWSER);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		browserButton.setLayoutData(data);
		browserButton.addSelectionListener(new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(parent.getShell(), SWT.OPEN);
				fileDialog.setFilterExtensions(new String[] {"*.xls"});
				fileDialog.setFilterNames(new String[] {"Excel File(*.xls)"});
				String filterPath = fileText.getText();
				if(filterPath != null){
					int last = filterPath.lastIndexOf("\\");
					if(last > 0)
						fileDialog.setFilterPath(filterPath.substring(0, last));
				}
				filePath = fileDialog.open();				
				fileText.setText(filePath);
				
				testConnectionUrl();
				
				setPageComplete(isPageComplete());
				getContainer().updateButtons();
			}

			private void testConnectionUrl() {
				SourceConnection connection = new SourceConnection(Op.toLocaleString(filePath));
				try {
					connection.connection();
					
					try {
						tableName.setItems(connection.getTables());
					} catch (SQLException e) {
						setErrorMessage(MessageHelper.WIZARD_LOCATIONPAGE_FETCHTABLE_ERROR);
					}
				} catch (SQLException e) {
					setErrorMessage(MessageHelper.WIZARD_LOCATIONPAGE_CONNECTION_ERROR);
				}finally{
					try {
						connection.dispose();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
			
		});
		//combo for table name
        label = new Label(parent, SWT.NONE);
        data = new GridData();
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        label.setText(MessageHelper.WIZARD_LOCATIONPAGE_TABLE_LABEL);
        
        tableName = new Combo(parent, SWT.READ_ONLY);
        data = new GridData();
        data.horizontalSpan = 2;
        tableName.setLayoutData(data);
        tableName.addSelectionListener(new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent e) {		
			}

			public void widgetSelected(SelectionEvent e) {
				setPageComplete(isPageComplete());
				getContainer().updateButtons();
			}
			
		});
        
		final Button ignoreCheck = new Button(parent, SWT.CHECK);
		ignoreCheck.setText(MessageHelper.WIZARD_IMPORT_FILEPAGE_CHECK);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		data.horizontalSpan = 2;
		ignoreCheck.setLayoutData (data);
		ignoreCheck.setSelection(bOverwrite );
		ignoreCheck.addSelectionListener(new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent e) {	
			}

			public void widgetSelected(SelectionEvent e) {
				bOverwrite = !bOverwrite;
			}
		});        
        
        setControl(parent);
	}
	public boolean isPageComplete() {
		if (getErrorMessage() != null) return false;
		if(isTextNonEmpty(fileText) && tableName.getSelectionIndex() >= 0){
			ImportWizard wizard = (ImportWizard)getWizard();
			wizard.fileCompleted = true;
			return true;
		}
		return false;
	}
	
	private static boolean isTextNonEmpty(Text t)
	{
		String s = t.getText();
		if ((s!=null) && (s.trim().length() >0)) return true;
		return false;
	}	
	
	public String getFilePath(){
		return Op.toLocaleString(filePath);
	}
	
	public String getTableName(){
		return Op.toLocaleString(tableName.getText());
	}

	public boolean isOverwrite(){
		return bOverwrite;
	}
}
